package mil.cdc.windows;

import java.awt.Toolkit;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import mil.cdc.listeners.CustomMouseListener;
import mil.cdc.main.Main;
import mil.cdc.utils.CDCConstants;
import mil.cdc.utils.Printer;

import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class PrintWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	private JLabel nameLbl;
	private JTextField nameField;
	private JButton printBtn;
	private JButton cancelBtn;
	private String printString;
	
	public PrintWindow(String printString) {
		this.printString = printString;
		initComponents();
	}

	private void initComponents() {
		setTitle("Print Window");
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/mil/cdc/data/ico/af.gif")));
		setLayout(new GroupLayout());
		add(getNameLbl(), new Constraints(new Leading(12, 12, 12), new Leading(12, 12, 12)));
		add(getNameField(), new Constraints(new Bilateral(12, 12, 4), new Leading(34, 12, 12)));
		add(getCancelBtn(), new Constraints(new Leading(155, 12, 12), new Leading(60, 12, 12)));
		add(getPrintBtn(), new Constraints(new Leading(68, 75, 12, 12), new Leading(60, 12, 12)));
		setSize(320, 139);
	}

	private JButton getCancelBtn() {
		if (cancelBtn == null) {
			cancelBtn = new JButton();
			cancelBtn.setText("Cancel");
			cancelBtn.addMouseListener(new CustomMouseListener(this, CDCConstants.CLOSE));
		}
		return cancelBtn;
	}
	
	private JButton getPrintBtn() {
		if (printBtn == null) {
			printBtn = new JButton();
			printBtn.setText("Print");
			printBtn.addMouseListener(new CustomMouseListener(this, CDCConstants.PRINT));
		}
		return printBtn;
	}

	private JTextField getNameField() {
		if (nameField == null) {
			nameField = new JTextField();
			nameField.setToolTipText("Enter Name");
		}
		return nameField;
	}

	private JLabel getNameLbl() {
		if (nameLbl == null) {
			nameLbl = new JLabel();
			nameLbl.setText("Name:");
		}
		return nameLbl;
	}
	
	public void print() {		
		
		String name = nameField.getText().trim();
		
		StringBuilder sb = new StringBuilder(name);
		sb.append("\n----------------------------------------------------\n");
		sb.append(printString);
		sb.append("\n----------------------------------------------------\n");
		sb.append("Date Taken: " + new Date().toString());
		
		Printer p = new Printer();
		
		if(p.print(sb.toString())) {
			this.dispose();
		}
		else {
			Main.errorWindow.displayErrorMessage("Unable to Print");
		}
	}

	public PrintWindow() {
		initComponents();
	}
	
}
